#include "PatientListWidget.h"
#include <QRect>
#include <QMouseEvent>
#include <QVBoxLayout>
#include "../../ServiceManager/LoadPatientList/PatientModel.h"
#include "EditPatientInfo.h"
#include "DeletePatient.h"

namespace UserManagementSystem {



PatientListWidget::PatientListWidget(UserRepository *repository, QWidget *parent)
    :QWidget(parent)
{

    qDebug()<<__FUNCTION__;
    m_repority=repository;

    m_mainLayout=new QVBoxLayout(this);

    //设置边框
    int dx=this->height()/20;
    qDebug()<<"dx:"<<dx;
    m_mainLayout->setContentsMargins(dx,dx,dx,0);

    m_listView=new QListView();
    m_listView->setMouseTracking(true);




    m_patientModel=new PatientModel(repository,nullptr);
    m_patientDelegate=new PatientDelegate();


    m_listView->setModel(m_patientModel);
    m_listView->setItemDelegate(m_patientDelegate);


    SetDefaultLayout();

    //绑定信号槽
    QObject::connect(m_patientDelegate, &PatientDelegate::On_EditPersonData_Btn, [&](int row){
        // QMessageBox::information(this, "按钮1", QString("你点击了第 %1 行的按钮1").arg(row));

        //点击按钮之后，new一个窗口，在窗口中编辑信息之后，将此乃西存储在

        EditPatientInfo* editInfoWidget=new EditPatientInfo(nullptr,m_repority,row);
        editInfoWidget->show();

    });

    QObject::connect(m_patientDelegate, &PatientDelegate::On_DeletePerson_Btn, [&](int row){
        // QMessageBox::information(this, "按钮2", QString("你点击了第 %1 行的按钮2").arg(row));
        DeletePatient* deletePatient=new DeletePatient();
        deletePatient->show();
    });
}

void PatientListWidget::SetDefaultStyle()
{
   // m_listView->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding);
    m_listView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
    m_listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 防止超宽滚动

    int each_heigh=this->height()/6;
    int each_width=this->width();
    qDebug()<<"each_height:"<<each_heigh<<","<<"each_width:"<<each_width;

    m_listView->setSpacing(5);
    m_listView->setUniformItemSizes(true);//item设置为同一高度
    m_listView->setGridSize(QSize(each_width-50,each_heigh));//设置宽高
    m_listView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QString style = QString(R"(
    QListView::item { height: %1px; width: %2px; }
    QScrollBar:vertical {
        background: transparent;
        width: 4px;
        margin: 0px 0px 0px 0px;
    }

    QScrollBar::handle:vertical {
        background: lightgray;
        min-height: 200px;
        width: 4px;
        border-radius: 2px;
    }

    QScrollBar::sub-line, QScrollBar::add-line,
    QScrollBar::up-arrow, QScrollBar::down-arrow:vertical {
        height: 0px;
    }
)").arg(each_heigh).arg(each_width-50);

    m_listView->setStyleSheet(style);
}

void PatientListWidget::SetDefaultLayout()
{
    m_mainLayout->addWidget(m_listView);
    qDebug()<<this->rect();

    SetDefaultStyle();
}

};
